package com.ruoyi.platform.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.merchant.domain.Product;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.merchant.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.platform.mapper.DishAuditMapper;
import com.ruoyi.platform.domain.DishAudit;
import com.ruoyi.platform.service.IDishAuditService;

/**
 * 商家菜品上架审核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class DishAuditServiceImpl implements IDishAuditService
{
    @Autowired
    private DishAuditMapper dishAuditMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商家菜品上架审核
     *
     * @param auditId 商家菜品上架审核主键
     * @return 商家菜品上架审核
     */
    @Override
    public DishAudit selectDishAuditByAuditId(Long auditId)
    {
        return dishAuditMapper.selectDishAuditByAuditId(auditId);
    }

    /**
     * 查询商家菜品上架审核列表
     *
     * @param dishAudit 商家菜品上架审核
     * @return 商家菜品上架审核
     */
    @Override
    public List<DishAudit> selectDishAuditList(DishAudit dishAudit)
    {
        return dishAuditMapper.selectDishAuditList(dishAudit);
    }

    /**
     * 新增商家菜品上架审核
     *
     * @param dishAudit 商家菜品上架审核
     * @return 结果
     */
    @Override
    public int insertDishAudit(DishAudit dishAudit)
    {
        dishAudit.setCreateTime(DateUtils.getNowDate());
        dishAudit.setApplyTime(DateUtils.getNowDate());
        dishAudit.setAuditStatus("0"); // 默认待审核状态
        return dishAuditMapper.insertDishAudit(dishAudit);
    }

    /**
     * 修改商家菜品上架审核
     *
     * @param dishAudit 商家菜品上架审核
     * @return 结果
     */
    @Override
    public int updateDishAudit(DishAudit dishAudit)
    {
        dishAudit.setUpdateTime(DateUtils.getNowDate());
        return dishAuditMapper.updateDishAudit(dishAudit);
    }

    /**
     * 批量删除商家菜品上架审核
     *
     * @param auditIds 需要删除的商家菜品上架审核主键
     * @return 结果
     */
    @Override
    public int deleteDishAuditByAuditIds(Long[] auditIds)
    {
        return dishAuditMapper.deleteDishAuditByAuditIds(auditIds);
    }

    /**
     * 删除商家菜品上架审核信息
     *
     * @param auditId 商家菜品上架审核主键
     * @return 结果
     */
    @Override
    public int deleteDishAuditByAuditId(Long auditId)
    {
        return dishAuditMapper.deleteDishAuditByAuditId(auditId);
    }


    /**
     * 审核通过
     *
     * @param auditId 审核ID
     * @param auditorId 审核人ID
     * @param auditorName 审核人姓名
     * @param auditRemark 审核备注
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int approveDish(Long auditId, Long auditorId, String auditorName, String auditRemark) {
        // 1. 查询审核记录
        DishAudit audit = dishAuditMapper.selectDishAuditByAuditId(auditId);
        if (audit == null) {
            throw new ServiceException("审核记录不存在");
        }

        if (!"0".equals(audit.getAuditStatus())) {
            throw new ServiceException("该记录已审核，请勿重复操作");
        }

        if (audit.getDishId() == null) {
            throw new ServiceException("商品ID不存在，无法审核");
        }

        // 2. 更新审核记录状态
        DishAudit updateAudit = new DishAudit();
        updateAudit.setAuditId(auditId);
        updateAudit.setAuditStatus("1"); // 审核通过（字符串类型）
        updateAudit.setAuditorId(auditorId);
        updateAudit.setAuditorName(auditorName);
        updateAudit.setAuditTime(new Date());
        updateAudit.setAuditRemark(auditRemark);
        updateAudit.setUpdateTime(DateUtils.getNowDate());

        int result = dishAuditMapper.updateAuditStatus(updateAudit);

        // 3. 同步更新商品表的审核状态
        if (result > 0) {
            Product product = new Product();
            product.setProductId(audit.getDishId());
            product.setMerchantId(audit.getMerchantId());
            product.setAuditStatus(1); // 审核通过（整型）
            productMapper.updateProduct(product);
        }

        return result;
    }

    /**
     * 审核拒绝
     *
     * @param auditId 审核ID
     * @param auditorId 审核人ID
     * @param auditorName 审核人姓名
     * @param rejectReason 拒绝原因
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int rejectDish(Long auditId, Long auditorId, String auditorName, String rejectReason) {
        // 1. 查询审核记录
        DishAudit audit = dishAuditMapper.selectDishAuditByAuditId(auditId);
        if (audit == null) {
            throw new ServiceException("审核记录不存在");
        }

        if (!"0".equals(audit.getAuditStatus())) {
            throw new ServiceException("该记录已审核，请勿重复操作");
        }

        if (audit.getDishId() == null) {
            throw new ServiceException("商品ID不存在，无法审核");
        }

        // 2. 更新审核记录状态
        DishAudit updateAudit = new DishAudit();
        updateAudit.setAuditId(auditId);
        updateAudit.setAuditStatus("2"); // 审核拒绝（字符串类型）
        updateAudit.setAuditorId(auditorId);
        updateAudit.setAuditorName(auditorName);
        updateAudit.setAuditTime(new Date());
        updateAudit.setRejectReason(rejectReason);
        updateAudit.setUpdateTime(DateUtils.getNowDate());

        int result = dishAuditMapper.updateAuditStatus(updateAudit);

        // 3. 同步更新商品表的审核状态
        if (result > 0) {
            Product product = new Product();
            product.setProductId(audit.getDishId());
            product.setMerchantId(audit.getMerchantId());
            product.setAuditStatus(2); // 审核未通过（整型）
            productMapper.updateProduct(product);
        }

        return result;
    }

    /**
     * 根据商家ID查询待审核菜品列表
     *
     * @param merchantId 商家ID
     * @return 菜品审核列表
     */
    @Override
    public List<DishAudit> selectPendingDishByMerchantId(Long merchantId)
    {
        return dishAuditMapper.selectPendingDishByMerchantId(merchantId);
    }

    /**
     * 获取审核统计数据
     *
     * @return 统计数据
     */
    @Override
    public Map<String, Object> getAuditStatistics()
    {
        Map<String, Object> statistics = new HashMap<>();

        // 统计各状态的数量
        int pending = dishAuditMapper.countByAuditStatus(0); // 待审核
        int approved = dishAuditMapper.countByAuditStatus(1); // 已通过
        int rejected = dishAuditMapper.countByAuditStatus(2); // 已拒绝

        statistics.put("pending", pending);
        statistics.put("approved", approved);
        statistics.put("rejected", rejected);

        // 计算总数
        int total = pending + approved + rejected;
        statistics.put("total", total);

        return statistics;
    }

    /**
     * 批量审核通过
     *
     * @param auditIds 审核ID数组
     * @param auditorId 审核人ID
     * @param auditorName 审核人姓名
     * @param auditRemark 审核备注
     * @return 结果
     */
    @Override
    @Transactional
    public int batchApproveDish(Long[] auditIds, Long auditorId, String auditorName, String auditRemark)
    {
        int result = 0;
        for (Long auditId : auditIds) {
            result += approveDish(auditId, auditorId, auditorName, auditRemark);
        }
        return result;
    }

    /**
     * 批量审核拒绝
     *
     * @param auditIds 审核ID数组
     * @param auditorId 审核人ID
     * @param auditorName 审核人姓名
     * @param rejectReason 拒绝原因
     * @return 结果
     */
    @Override
    @Transactional
    public int batchRejectDish(Long[] auditIds, Long auditorId, String auditorName, String rejectReason)
    {
        int result = 0;
        for (Long auditId : auditIds) {
            result += rejectDish(auditId, auditorId, auditorName, rejectReason);
        }
        return result;
    }
}
